//
//  StoryScrollPageViewController.m
//  PetTown
//
//  Created by lanou3g on 15/5/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "StoryScrollPageViewController.h"
#import "UIImageView+WebCache.h"
#import "StoryView.h"
@interface StoryScrollPageViewController ()
{
     MBProgressHUD * HUD;
}
@end

@implementation StoryScrollPageViewController

-(void)loadView
{

    self.stoView = [[StoryView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    self.view = _stoView;
 
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Mohu];

    //返回
    [self returns];
    [self imageJSON];
  
}

//设置返回按钮
- (void)returns{
    UIButton * returnBu = [UIButton buttonWithType:(UIButtonTypeSystem)];
    returnBu.frame = CGRectMake(0, 0, 32, 32);
    [returnBu setBackgroundImage:[UIImage imageNamed:@"ReturnPage.png"] forState:UIControlStateNormal];
    [returnBu addTarget:self action:@selector(returnBueeon:) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem * leftBu = [[UIBarButtonItem alloc] initWithCustomView:returnBu];
    self.navigationItem.leftBarButtonItem = leftBu;
}
//点击返回按钮
- (void)returnBueeon:(UIBarButtonItem *)leftButton{
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark------数据解析
- (void)imageJSON
{
    //创建大菊花加载
    HUD  = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    [HUD hide:NO afterDelay:1.0];
    HUD.labelText = @"正在加载";
    [HUD show:YES];

    NSString * str = [NSString stringWithFormat:StoryDetailsAPI,_ppid];

    NSURL * url = [NSURL URLWithString:str];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestReloadIgnoringCacheData) timeoutInterval:60.0];
    
    __weak StoryScrollPageViewController * storyScrollPageVC = self;
    
    storyScrollPageVC.photoArray = [NSMutableArray array];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil) {
            return ;
        }

        NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];

       
        NSMutableDictionary * dic1 = dic[@"data"];
        storyScrollPageVC.strTime = dic1[@"addtime"];
        storyScrollPageVC.titleText = dic1[@"title"];

        NSArray * Array = dic1[@"content"];
        
        for (NSDictionary * modelDic in Array) {
            
            Story * story = [[Story alloc]init];
            [story setValuesForKeysWithDictionary:modelDic];
            [storyScrollPageVC.photoArray addObject:story];

        }

        [self.view reloadInputViews];
        [self setSubViews];
    }];
}

#pragma mark-------添加滚动视图
- (void)setSubViews
{
   
    _stoView.storyScrollView.delegate = self;
    self.sumWid = 0;
    
    for (int i = 0; i<_photoArray.count+1; i++) {

        if (i == 0) {
            
            [self firstPhotoInfo];
            
        }else{
            
            Story * story = _photoArray[i-1];
            NSURL * url = [NSURL URLWithString:story.photo];
            NSData * imgData = [NSData dataWithContentsOfURL:url];
            UIImage * img = [UIImage imageWithData:imgData];
            CGFloat wid = 0;

            if (nil == url) {
                wid = self.view.frame.size.width-80;
            //storyScrollView白色半透明背景
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(_stoView.storyScrollView.frame)+_sumWid-36, 0, wid, CGRectGetHeight(_stoView.storyScrollView.frame))];
                label.backgroundColor = [UIColor whiteColor];
                label.alpha = 0.4;
                [_stoView.storyScrollView addSubview:label];
       
            }else{
                wid = CGRectGetHeight(_stoView.storyScrollView.frame) * img.size.width/img.size.height ;

            }
            
            self.photosView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(_stoView.storyScrollView.frame)+_sumWid-36, 0, wid, CGRectGetHeight(_stoView.storyScrollView.frame))];
            
            [_photosView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
            
            
           //设置边框
            CALayer * layer = [_photosView layer];
            layer.borderColor = [UIColor whiteColor].CGColor;
            layer.borderWidth = 5.0f;

            [_stoView.storyScrollView addSubview:_photosView];
#pragma mark-----图片上的Label
            
            //图片上显示文字的Label
            _stoView.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(_sumWid+CGRectGetWidth(_stoView.storyScrollView.frame)+10-36, 5,wid-20, CGRectGetHeight(_stoView.storyScrollView.frame)-10)];
            //+8两图片中间中间距离
            self.sumWid += wid+8;
            
            _stoView.textLabel.text = story.content;
            //自动换行
            _stoView.textLabel.numberOfLines = 0;
            //居中
            _stoView.textLabel.textAlignment = NSTextAlignmentCenter;
           
            _stoView.textLabel.font = [UIFont boldSystemFontOfSize:15];
            
            [_stoView.storyScrollView addSubview:_stoView.textLabel];
        }
    }
    _stoView.storyScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)+_sumWid + CGRectGetWidth(self.view.frame)-72, CGRectGetHeight(_stoView.storyScrollView.frame));
    //大菊花加载
    //HUD = nil;
    [MBProgressHUD hideHUDForView:self.view animated:YES];
   

    
#pragma mark----最后一张图片
    [self LastPhotoInfo];
}

#pragma mark----最后一张图片
- (void)LastPhotoInfo
{
    UIImageView * imageLast = [[UIImageView alloc]initWithFrame:CGRectMake(_sumWid+ CGRectGetWidth(self.view.frame)-36, 0,  CGRectGetWidth(_stoView.storyScrollView.frame)-40,CGRectGetHeight(_stoView.storyScrollView.frame))];

UIImageView * EWMImage = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth(_stoView.storyScrollView.frame)-40,CGRectGetHeight(_stoView.storyScrollView.frame))];
    EWMImage.image = [UIImage imageNamed:@"last.png"];
    [imageLast addSubview:EWMImage];
    imageLast.backgroundColor = [UIColor whiteColor];
    [_stoView.storyScrollView addSubview:imageLast];
}

#pragma mark-------滚动视图第一张图片显示信息
- (void)firstPhotoInfo
{
    //第一张图片的白色背景图片
    UIImageView * imageBJ = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_stoView.storyScrollView.frame)-40,CGRectGetHeight(_stoView.storyScrollView.frame))];
    imageBJ.backgroundColor = [UIColor whiteColor];
    [_stoView.storyScrollView addSubview:imageBJ];
    
    //头字母图片
    UIImageView * ZMView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10,CGRectGetWidth(_stoView.storyScrollView.frame)-100, CGRectGetHeight(_stoView.storyScrollView.frame)/3.0f-40)];
    ZMView.image = [UIImage imageNamed:@"1433560952_983856.png"];
       [_stoView.storyScrollView addSubview:ZMView];
    
    //封面图片---------
    self.IMView = [[UIImageView alloc]initWithFrame:CGRectMake(30, CGRectGetHeight(_stoView.storyScrollView.frame)/3.0f-20,CGRectGetWidth(_stoView.storyScrollView.frame)-100, CGRectGetHeight(_stoView.storyScrollView.frame)/3.0f+30)];
    
    NSURL * url1 = [NSURL URLWithString:_photoURL];
    [_IMView sd_setImageWithURL:url1 placeholderImage:[UIImage imageNamed:@""]];
  
    //图片自适应
    _IMView.contentMode = UIViewContentModeScaleToFill;
    
    [_stoView.storyScrollView addSubview:_IMView];
    //内容---------
    UILabel * label1 =[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetHeight(_stoView.storyScrollView.frame)*2.0/3.0f+20, CGRectGetWidth(_stoView.storyScrollView.frame)-100, 25)];
    label1.text = _titleText;
    label1.textAlignment = NSTextAlignmentCenter;
    [_stoView.storyScrollView addSubview:label1];
    //时间---------
    UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetHeight(_stoView.storyScrollView.frame)*2.0/3.0f+45, CGRectGetWidth(_stoView.storyScrollView.frame)-100, 20)];
    
    label2.text = _strTime;//赋值
    label2.textColor = [UIColor grayColor];//颜色
    label2.font = [UIFont systemFontOfSize:13];//字体
    label2.textAlignment = NSTextAlignmentCenter;//居中
    [_stoView.storyScrollView addSubview:label2];
    
}

#pragma mark----添加首页图片 并设置模糊效果
- (void)Mohu
{
    //view的背景图片
    UIImageView * backImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    NSURL * backUrl = [NSURL URLWithString:_photoURL];
    [backImgView sd_setImageWithURL:backUrl placeholderImage:[UIImage imageNamed:@"place.jpg"]];
    [_stoView addSubview:backImgView];
    [_stoView bringSubviewToFront:_stoView.storyScrollView];
    
    //模糊效果
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:_photoURL]];
    //创建模糊视图对象
    UIVisualEffectView * visualEffectView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    //给模糊视图设置frame
    visualEffectView.frame = self.view.bounds;
    //添加模糊视图
    [backImgView addSubview:visualEffectView];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //清除缓存
    SDImageCache * sdI = [SDImageCache sharedImageCache];
    [sdI clearDisk];
    [sdI clearMemory];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
